package com.juno.cafenoa.member;

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.juno.cafenoa.Constants;
import com.juno.cafenoa.R;
import com.juno.cafenoa.TopNavigatorView;
import com.juno.cafenoa.mypage.PasswordActivity;
import com.juno.cafenoa.net.AsyncTaskCompleteListener;
import com.juno.cafenoa.net.HttpPostTask;
import com.juno.cafenoa.util.CafeNoaPreference;
import com.juno.cafenoa.util.Util;

public class AccountChangeActivity extends Activity implements OnClickListener{
	Context mContext;
	
	EditText phoneNumber;
	EditText userId;
	
	Button confirmBtn;
	Button resetBtn;
	
	TopNavigatorView navi;
		
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_change_user_phone);
		mContext = this;
		
		phoneNumber = (EditText) findViewById(R.id.update_phoneNumber);
		userId = (EditText) findViewById(R.id.update_id);
		navi = (TopNavigatorView) findViewById(R.id.navi_changeInformation);
		
		navi.init("회원정보 변경");
		
		userId.setText(CafeNoaPreference.getUserID(this));
		
		confirmBtn = (Button) findViewById(R.id.update_btn);
		resetBtn = (Button) findViewById(R.id.update_clear_btn);
		confirmBtn.setOnClickListener(this);
		resetBtn.setOnClickListener(this);
		
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.update_btn:
			//update버튼 눌릴시 -> 패스워드 확인화면으로 전환.
			Intent intent = new Intent(mContext, PasswordActivity.class);
			intent.putExtra(Constants.USER_PASSWORD_MODE_SET, Constants.PASSWORD_CONFIRM);
			startActivityForResult(intent, Constants.REQUEST_PASWORD_CONFIRM);
			break;
		case R.id.update_clear_btn:
			phoneNumber.setText("");
			break;
			
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if(requestCode==Constants.REQUEST_PASWORD_CONFIRM){
			boolean flag = data.getBooleanExtra(Constants.USER_PASSWORD_EQUAL, false);
			if(flag){
				//서버랑 통신해서 올림.
				phoneNumChangeListener.excuteHttpTask();
				finish();
			}
		}
	}
	
	AsyncTaskCompleteListener phoneNumChangeListener = new AsyncTaskCompleteListener(){

		@Override
		public void excuteHttpTask() {
			String url = Util.getUrl("MemberInFormChange");
			
			//id와 passwd 세팅해줌.
			String phone = phoneNumber.getText().toString();
						
			//name value pair에 id와 password 세팅
			ArrayList<NameValuePair> value = new ArrayList<NameValuePair>();
			value.add(new BasicNameValuePair("USER_ID", userId.getText().toString()));
			value.add(new BasicNameValuePair("USER_NEW_PHONE", phone));
			value.add(new BasicNameValuePair("USER_NEW_PASSWORD", ""));
			value.add(new BasicNameValuePair("USER_CHANGE_FLAG", "C"));
			
			//httpPostTast 실행.
			HttpPostTask http = new HttpPostTask(mContext, value, url, phoneNumChangeListener);
			http.execute();
		}

		@Override
		public void onTaskComplete(String result, boolean taskOk) {
			Log.v("change","result : " + result);
			if(taskOk){
				int code = -1;
				String resultString = null;
				
				try {
					//코드 받아옴. JSON형태.
					//파싱하는 과정
					JSONObject object = new JSONObject(result);
					code = object.getInt("RESULT");
					resultString = object.getString("RESULT_STRING");
				} catch (JSONException e) {
					e.printStackTrace();
				}
				//코드가 내려오지 않은경우는 서버 에러 !
				if(code==-1){
					Log.v("CafeNoa","Server Error No code!");
					return;
				}
				if(code==Constants.NETWORK_SUCCESS){
					Toast.makeText(mContext, "성공적으로 변경되었습니다.", Toast.LENGTH_LONG).show();
				}
				Log.v("AccountChange",resultString);			
			}
		}
		
	};
	
	

}
